#!/bin/bash

#inputfile=$1
dbname=$1
login=$2
passwd=$3
comref=$4
persofile=$5
pts=$6

function print_usage()
{
   echo "Usage: $0 <dbname> <login> <passwd> <commercial reference> <perso file> [<pts> ...]"
}

function get_err_msg()
{
    case "$1" in
	103)
	    echo "Could not connect to the Server."
	    ;;
	104)
	    echo "Input file does not exist."
	    ;;
	105)
	    echo "Could not get  Input file content."
	    ;;
	*)
	    echo "undefined"
	    ;;
    esac
}

if [ "x$pts" == "x" ]; then
   echo "`date +'%Y/%m/%d %H:%M:%S'` [ERROR] All arguments not available."
   print_usage
   exit 1
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file ..."
lockfile="/tmp/ftp1.cfg.lock" 
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting ..." 
`./sendmail.pl -f multisimgen.support@tcl.com -t frederic.chailloux@jrdcom.com -s "Multisimgen >ftp1.cfg.sh< notification" log`
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file ..."
`touch /tmp/ftp1.cfg.lock` 

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing ... "
#/local/tools/repository/php-4.4.4/bin/php ftp1.cfg.php $dbname $login $passwd $comref $persofile $pts
/local/tools/repository/php-4.4.4/bin/php ftp1.cfg.php $@

res=$?
result=`get_err_msg $res`
if [ $res -ne 0 ]; then
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Error $res : $result"
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of Process."
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file ..."
`rm /tmp/ftp1.cfg.lock` 

`cat ftp1.cfg.sh.log >> ftp1.cfg.log`

if [ $res -ne 0 ]; then
`./sendmail.pl -f multisimgen.support@tcl.com -t frederic.chailloux@jrdcom.com -s "Multisimgen >ftp1.cfg.sh< notification" ftp1.cfg.sh.log`
fi